import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Manage',
    component: () => import('../views/Manage.vue'),
    redirect: '/home',
    children:[
      {path:'/home',component: () => import('../views/Home.vue')},
      {path:'/computer',component: () => import('../views/Computer.vue')},
      {path:'/phone',component: () => import('../views/Phone.vue')},
      {path:'/necessary',component: () => import('../views/Necessary.vue')},
      {path:'/fileRank',component: () => import('../views/FileRank.vue')}
    ]
  },
  {
    path: '/fileInfo',
    name: 'FileInfo',
    component: () => import('../views/FileInfo.vue')
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue')
  },
  {
    path: '/adminManage',
    name: 'AdminManage',
    meta: {title: '管理员主页', adminAuth: true},
    component: () => import('../views/AdminManage'),
    redirect: '/adminManage/adminHome',
    children: [
      {path: 'adminHome', name: 'AdminHome', meta: {title: '管理员首页', adminAuth: true}, component: () => import('../views/AdminHome.vue')},
      {path: 'adminFileManage', name: 'AdminFileManage', meta: {title: '管理员文件管理', adminAuth: true}, component: () => import('../views/AdminFileManage.vue')},
      {path: 'adminFileTypeManage', name: 'AdminFileTypeManage', meta: {title: '文件分区管理', adminAuth: true}, component: () => import('../views/AdminFileTypeManage.vue')},
      {path: 'adminUserInfo', name: 'AdminUserInfo', meta: {title: '管理员用户信息', adminAuth: true}, component: () => import('../views/AdminUserInfo.vue')},
    ]
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to, from, next) => {
  if (to.meta.adminAuth) { // 判断该路由是否需要登录权限
    let token = localStorage.getItem("token");
    if (token == null) {
      next({path: "/login"});
      return;
    }
  }
  //不需要正常跳转
  next();
});
export default router
